﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

///<summary>
///
///</summary>
public class GameView : MonoBehaviour
{
    GameObject enemyFactory;
    GameObject TriggerWin;
    //游戏设置面板开启委托
    public UnityAction Open;
    //游戏设置关闭委托
    public UnityAction Close;
    //游戏设置退出委托
    public UnityAction Exit;
    //游戏设置重置委托
    public UnityAction Rest;
      

    //设置面板
    GameObject setPanel;
    //面板开启按钮
    GameObject btnSet;
    //面板父类
    GameObject panelParent;
    //继续游戏按钮
    GameObject btnContinueGame;
    //退出按钮
    GameObject btnExit;
    //重置按钮
    GameObject btnRest;
    //面板背景
    GameObject bg;

    //当前游戏得分
    GameObject txtGamePoint;
    //当前游戏所得金币
    GameObject txtGameMoney;

	//减速背景
	public GameObject retarderBg;
	//地板
	GameObject floor;
	//闪电
	public GameObject Lighting;


    public void OnEnable()
    {
        Debug.Log("播放音乐");
        AudioManager.GetInstance.PlayBGMusic();
    }

    void Start()
    {
        btnSet.GetComponent<Button>().onClick.AddListener(() =>
        {
            Open();
            PlaySound("Sounds/Panel/click_start");
        });
        btnContinueGame.GetComponent<Button>().onClick.AddListener(() =>
        {
            Close();
            PlaySound("Sounds/Panel/click_start");
        });
        btnRest.GetComponent<Button>().onClick.AddListener(() =>
        {
            Rest();
            PlaySound("Sounds/Panel/click_start");
        });
        btnExit.GetComponent<Button>().onClick.AddListener(() =>
        {
            PlaySound("Sounds/Panel/click_start");
            Exit();
        });
    }

    // Start is called before the first frame update
    public void Init()
    {
		GameConfig.IsGameOver = false;
        enemyFactory = GameObject.Find("EnemyFactory");
        TriggerWin = GameObject.Find("Canvas/GameView/BackGround/triggerWin");
        panelParent = GameObject.Find("Canvas/Panel");
        setPanel = panelParent.transform.Find("SettingPanel").gameObject;
        btnSet = GameObject.Find("Canvas/Panel/MainPanel/BtnSetting");
        btnContinueGame = setPanel.transform.Find("BtnPlay").gameObject;
        btnExit = setPanel.transform.Find("BtnExit").gameObject;
        btnRest = setPanel.transform.Find("BtnRest").gameObject;
        bg = panelParent.transform.Find("endBg").gameObject;
		floor = GameObject.Find("Canvas/GameView/BackGround/floor");
        //当前游戏所得
        txtGamePoint = GameObject.Find("Canvas/GameView/BackGround/txtGamePoint");
        txtGameMoney = GameObject.Find("Canvas/Panel/gameMoney/txtGameMoney");
	}
	
	//重新开始游戏
	public void RestGame()
    {
		TriggerWin.GetComponent<GroundEnemyWin>().RestGame();
        ClearGameScoreAndGameMoney();
        ClosePanel();
        GameConfig.EnemyCount = 0;
        if (GameObject.FindGameObjectsWithTag("Balloon").Length > 0)
        {
            GameObject[] balloons = GameObject.FindGameObjectsWithTag("Balloon");
            for(int i = 0; i < balloons.Length; i++)
            {
                balloons[i].GetComponent<BalloonInputKeyComponent>().RestGame();
            }
        }
        SetEnemyRecycle();
       
        //AudioManager.GetInstance.PlayBGMusic();
    }
    public void AllEnemyDontDestroy()
    {
        enemyFactory.GetComponent<EnemyFactory>().EnemyDontDestroy();
    }


    //开启面板
    public void OpenPanel()
    {
		AudioManager.GetInstance.Play("Sounds/Panel/click_start");
		setPanel.SetActive(!setPanel.activeInHierarchy);
        bg.SetActive(true);
        if(!setPanel.activeInHierarchy)
        {
            Time.timeScale = 1f;
        }
        else
        {
            Time.timeScale = 0f;
        }
    }
    //关闭面板
    public void ClosePanel()
    {
		AudioManager.GetInstance.Play("Sounds/Panel/click_start");
		setPanel.SetActive(false);
        bg.SetActive(false);
    }

    public void SetEnemyRecycle()
    {
        enemyFactory.GetComponent<EnemyFactory>().EnemyRecycle();
        Debug.Log("回收怪物");
    }
    public void ClearGameScoreAndGameMoney()
    {
        PlayerDataManager.GetIntance().Clear();
        txtGamePoint.GetComponent<Text>().text = PlayerDataManager.GetIntance().GetPoint().ToString();
        txtGameMoney.GetComponent<Text>().text = PlayerDataManager.GetIntance().GetMoney().ToString();
    } 
    
    public void AddPoint()
    {
        PlayerDataManager.GetIntance().AddPoint(1);
        //游戏得分
        txtGamePoint.GetComponent<Text>().text = PlayerDataManager.GetIntance().GetPoint().ToString();
    }

	


	public void AddMoney(int num)
    {
        PlayerDataManager.GetIntance().AddMoney(num);
        txtGameMoney.GetComponent<Text>().text = PlayerDataManager.GetIntance().GetMoney().ToString();
    }
    
	void PlaySound(string path)
	{
		AudioManager.GetInstance.Play(path);
	}

	public void Retarder()
	{
		retarderBg.SetActive(true);
		StartCoroutine(TimerAnimation("Rearder"));
	}
	IEnumerator TimerAnimation(string PropState)
	{   
		float counter = 0;
		switch (PropState)
		{
			case "Rearder":
				while (counter <= 3)
				{
					counter += Time.fixedDeltaTime;
					if (GameConfig.IsGameOver)
					{
						counter = 3;
					}
					yield return new WaitForFixedUpdate();
				}
				Time.timeScale = 1;
				retarderBg.SetActive(false);
				break;
			case "SpeedUp":
				while (counter <= 4)
				{
					counter += Time.fixedDeltaTime;
					yield return new WaitForFixedUpdate();
				}
				retarderBg.SetActive(false);
				floor.tag = "floor";
				GameConfig.SpeedState = false;
				break;
		}
	}
	public void Speed()
	{
		floor.tag = "die";
		retarderBg.SetActive(true);
		GameConfig.SpeedState = true;
		StartCoroutine(TimerAnimation("SpeedUp"));
	}
	public void AwakeLightning()
	{
		Lighting.SetActive(true);
	}
	public void ClearOneEnemy()
	{
		enemyFactory.GetComponent<EnemyFactory>().ClearOne();
	}
	public void ClearOnBalloonType()
	{
		enemyFactory.GetComponent<EnemyFactory>().ClearOneBalloonType();
	}
}
